package com.uwo.commons.kits;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间格式转换
 * @author hao.yan
 */
public class DateKits {

    /**
     * 时间格式
     * @param pattern
     * @param time
     * @return
     */
    public static String dateFormat(String pattern, Long time){
        return dateFormat(pattern, new Date(time));
    }

    /**
     * 时间格式
     * @param pattern
     * @param date
     * @return
     */
    public static String dateFormat(String pattern, Date date){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        format.format(date);
        return format.toString();
    }

    /**
     * 时间格式解析
     * @param pattern
     * @param dateTime
     * @return
     */
    public static Date dateParse(String pattern, String dateTime){
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取周几
     * @param date
     * @return
     */
    public static int week(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return w;
    }


}
